test_data = dict(
    pic_6_15_a_1=[15, 6, ],
    pic_6_15_a_2=[10, 8, 7, 2, 3, 6, ],
    pic_6_15_a_3=[21, 10, 12, ],
    pic_6_15_b_1=[6, 15, ],
    pic_6_15_b_2=[10, 2, 7, 8, 3, 6, ],
    pic_6_15_b_3=[12, 10, 21, ],
    my_1=[],
    my_2=[100],
    my_3=[100, 97, 98],
    my_4=[97, 98, 100],
)


def left_child(i):
    assert (isinstance(i, int), i >= 0)
    return 2 * i + 1


def right_child(i):
    assert (isinstance(i, int), i >= 0)
    return 2 * i + 2


def parent(i):
    assert(isinstance(i, int), i >= 0)
    if i % 2 == 0:
        if i <= 0:
            return None
        return (i - 2) // 2
    else:
        return (i - 1) // 2


def is_heap(data, is_max=True):
    n = len(data)
    if 0 == n:
        return True
    elif 1 == n:
        return True
    """
    n => right border of left node's parent
    1 => x
    2 => 0
    3 => 0
    4 => 1
    5 => 1
    6 => 2
    7 => 2
    """
    for i in range(1 + (n - 2) // 2):
        if is_max:
            if data[i] < data[left_child(i)]:
                return False
    """
    n => right border of right node's parent
    1 => x
    2 => x
    3 => 0
    4 => 0
    5 => 1
    6 => 1
    7 => 2
    """
    if 2 == n:
        return True
    for i in range(1 + (n - 3) // 2):
        if is_max:
            if data[i] < data[right_child(i)]:
                return False
    return True


if __name__ == '__main__':

    for k, v in test_data.items():
        print(k, is_heap(v))
